<?php
/**
 * @file views-view-table.tpl.php
 * Template to display a view as a table.
 *
 * - $title : The title of this group of rows.  May be empty.
 * - $rows: An array of row items. Each row is an array of content
 *   keyed by field ID.
 * - $header: an array of headers(labels) for fields.
 * - $themed_rows: a array of rows with themed fields.
 * @ingroup views_templates
 */

?>
<?php foreach ($themed_rows as $count => $row): ?>
  <<?php print $item_node; ?>>
<?php foreach ($row as $field => $content): ?>
	
	<?php if($field == 'field_article_content') { ?>
	<description>
		<?php

			//foreach ($rows as $row_key => $row_value) {
			
				foreach($rows[$count]->field_field_article_content as $collection_key => $collection_value) {

					//get the field_collection id
					$collection_id = $collection_value['raw']['value'];

					//get the whole field_collection
					$loaded_collection = $collection_value['rendered']['entity']['field_collection_item'][$collection_id];

		?>
				<?php 
					//get the image url
					if(isset($loaded_collection['field_image'])) {
						$image_url = file_create_url($loaded_collection['field_image']['#items'][0]['uri']); 
						$image_title = $loaded_collection['field_image'][0]['#item']['title'];
						$image_alt = $loaded_collection['field_image'][0]['#item']['alt'];
				?>
						<image url="<?php print $image_url;?>" alt="<?php print $image_alt;?>" title="<?php print $image_title;?>" />
				<?php } //end if for image ?>


				<?php 
					//get the video url
					if(isset($loaded_collection['field_video'])) {  
						$video_link = $loaded_collection['field_video']['#items'][0]['video_url'];
				?>

						<Video>
							<name><?php print $video_link?></name>
							<width>640</width>
							<height>360</height>
						</Video>
				<?php } // end if for video?>

				<?php if(isset($loaded_collection['field_body'])): ?>
					<body><?php print htmlspecialchars($loaded_collection['field_body']['#items'][0]['value'], ENT_QUOTES, 'UTF-8'); ?></body>
				<?php endif; //end if for body field ?>

				<?php if(isset($loaded_collection['field_images'])): ?>
					<?php foreach ($loaded_collection['field_images']['#items'] as $image_key => $image_value) { 
						$other_image_url = file_create_url($image_value['uri']); 
						$other_image_title = htmlspecialchars($image_value['title'], ENT_QUOTES, 'UTF-8');
						$other_image_alt = $image_value['alt'];
					?>
						<image url="<?php print $other_image_url;?>" alt="<?php print $other_image_alt;?>" title="<?php print $other_image_title;?>" />
					<?php } ?>
				<?php endif; //end if for other images field?>

		<?php 
				} //end foreach loop for each row
			//} //end foreach loop for rows
		?>

	<?php  } else { ?>

    	<<?php print $xml_tag[$field]; ?>><?php print $content; ?></<?php print $xml_tag[$field]; ?>>
    <?php } ?>

<?php endforeach; ?>

	<?php 
		$node = $rows[$count]->_field_data['nid']['entity'];

		if(!empty($node->field_tags['und'])) {
			$tid_array = array();
			foreach ($node->field_tags['und'] as $term_key => $term_value) {
				$tid_array[] = $term_value['tid'];
			}
			//call custom function to get nid array of related content
			$related_articles = _get_related_content($node->nid, $tid_array);
		}
		
		
		if(!empty($related_articles)) {

	?>
		<related>
			<?php 
				global $base_url;
				$related_output = '<h2>Related Articles</h2><ul>';
				foreach ($related_articles['node'] as $node_key => $node_value) {
					$loaded_node = node_load($node_key);
					$node_title = $loaded_node->title;
					$node_alias = drupal_get_path_alias('node/'.$node_key);
					$node_full_path = $base_url . '/' . $node_alias;

					$related_output .= '<li><a href="'. $node_full_path .'" target="_blank">' . $node_title . '</a>';
				} //end foreach loop 
				$related_output .= '</ul>';

				print htmlspecialchars($related_output, ENT_QUOTES, 'UTF-8');
			?>

		</related>
	<?php } //end if for $related_articles ?>
</description>
  </<?php print $item_node; ?>>
<?php endforeach; ?>
